home *** CD-ROM | disk | FTP | other *** search
/ Aminet 24 / Aminet 24 (1998)(GTI - Schatztruhe)[!][Apr 1998].iso / Aminet / dev / c / cpost_1_4.lha / cpost.c < prev    next >
C/C++ Source or Header  |  1995-05-09  |  32KB  |  915 lines

  1. /*------------------------------------------------------------------
  2.  * cPost.c  : c language formatter
  3.  *------------------------------------------------------------------
  4.  * 10-10-91 originally by Patrick J. Mueller
  5.  * 12-03-92 converted from cBook to cPost
  6.  *------------------------------------------------------------------*/
  7.  
  8. #define PROGRAM_VERS  "1.4"
  9. #define PROGRAM_NAME  "cPost"
  10. #define PROGRAM_YEAR  "1994"
  11. #define PROGRAM_AUTH  "Patrick J. Mueller"
  12. #define PROGRAM_ADDR  "(pmuellr@vnet.ibm.com)"
  13. #define PROGRAM_ENVV  "CPOST"
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdarg.h>
  19. #include <signal.h>
  20. #include <time.h>
  21.  
  22. #include "parsearg.h"
  23.  
  24. #include "ctok.h"
  25. #define DEFINE_GLOBALS
  26. #include "cpost.h"
  27. #include "tokfile.h"
  28.  
  29. #include "cposthdr.h"
  30.  
  31. /*---------------------------------------------------------------
  32.  * global variables
  33.  *---------------------------------------------------------------*/
  34. int  AllDone = 0;
  35.  
  36. static char *ReservedTokens[] =
  37.    {
  38.    /*---------------------------------------------------------------
  39.     * data types
  40.     *---------------------------------------------------------------*/
  41.    "auto", "char", "const", "double", "enum", "extern", "float", "int",
  42.    "long", "register", "short", "signed", "static", "struct", "union",
  43.    "unsigned", "void", "volatile",
  44.  
  45.    /*---------------------------------------------------------------
  46.     * other keywords
  47.     *---------------------------------------------------------------*/
  48.    "break", "case", "continue", "default", "do", "else", "for", "goto",
  49.    "if", "return", "sizeof", "switch", "typedef", "while",
  50.  
  51.    /*---------------------------------------------------------------
  52.     * saa c extensions
  53.     *---------------------------------------------------------------*/
  54.    "_Packed","_System","_Optlink", "_Far16", "_Cdecl", "_Pascal"
  55.    };
  56.  
  57. /*------------------------------------------------------------------
  58.  * c++ reserved words
  59.  *------------------------------------------------------------------*/
  60. static char *CppReservedTokens[] =
  61.    {
  62.    "catch", "class", "delete", "friend", "inline", "new", "operator",
  63.    "private", "protected", "public", "template", "this", "throw", "try",
  64.    "virtual"
  65.    };
  66.  
  67.  
  68. /*------------------------------------------------------------------
  69.  * generate an error message and exit
  70.  *------------------------------------------------------------------*/
  71. void cPostError(
  72.    int   exitCode,
  73.    char *format,
  74.    ...
  75.    )
  76.    {
  77.    va_list vlist;
  78.  
  79.    fprintf(stderr,"%s : ",PROGRAM_NAME);
  80.  
  81.    va_start(vlist,format);
  82.    vfprintf(stderr,format,vlist);
  83.    va_end(vlist);
  84.  
  85.    fprintf(stderr,"\n");
  86.  
  87.    if (exitCode)
  88.       exit(exitCode);
  89.    }
  90.  
  91. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  92. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  93.  
  94. /*------------------------------------------------------------------
  95.  * print some help, assuming me is argv[0]
  96.  *------------------------------------------------------------------*/
  97. static void Usage(void)
  98.    {
  99.    fprintf(stderr,"%s %s by %s %s\n",PROGRAM_NAME,PROGRAM_VERS,PROGRAM_AUTH,PROGRAM_ADDR);
  100.    fprintf(stderr,"\n");
  101.    fprintf(stderr,"usage:\n");
  102.    fprintf(stderr,"   %s <options> <filespec> <filespec> ...\n",PROGRAM_NAME);
  103.    fprintf(stderr,"is used to produce a listing of C language files in PostScript\n");
  104.    fprintf(stderr,"format.  The PostScript output is written to stdout.\n\n");
  105.    fprintf(stderr,"where:\n");
  106.    fprintf(stderr,"   <filespec> is a filespec matching C language files.\n\n");
  107.    fprintf(stderr,"Valid options are:\n");
  108.    fprintf(stderr,"   -b[+|-]             - enable/disable bracketing around braces\n");
  109.    fprintf(stderr,"   -cext1,ext2,...     - treat files with extention ext1 and ext2 as C files\n");
  110.    fprintf(stderr,"   -d[+|-]             - enable/disable duplex\n");
  111.    fprintf(stderr,"   -hext1,ext2,...     - treat files with extention ext1 and ext2 as H files\n");
  112.    fprintf(stderr,"   -ifile1;file2;...   - imbed files into output\n");
  113.    fprintf(stderr,"   -kk1,k2,...         - treat k1, k2 as reserved (key=c++ adds c++ keywords)\n");
  114.    fprintf(stderr,"   -n#                 - separate line numbers from lines with # spaces\n");
  115.    fprintf(stderr,"   -n0                 - do not generate line numbers\n");
  116.    fprintf(stderr,"   -ofile              - output written to file (instead of stdout)\n");
  117.    fprintf(stderr,"   -p[+|-]             - enable/disable page break at functions\n");
  118.    fprintf(stderr,"   -rfile1;file2,...   - replace default PS procs with contents of files\n");
  119.    fprintf(stderr,"   -snt or -stn        - sort files by name/type or type/name\n");
  120.    fprintf(stderr,"   -t#                 - expand tabs to # columns\n");
  121.    fprintf(stderr,"   -wf1,f2;f3,f4       - prepend files f1, f2 to output, append files f3, f4\n");
  122.    fprintf(stderr,"   -xx,y               - coordinates for page adjustment\n");
  123.    fprintf(stderr,"   -ypath              - path to use for temporary files\n");
  124.    fprintf(stderr,"   -?                  - display this help\n\n");
  125.    fprintf(stderr,"Default options are:\n");
  126.    fprintf(stderr,"   -b+ -d- -cc -hh -n2 -p+ -stn -t4 -x0,0\n");
  127.    fprintf(stderr,"Options may also be set in the environment variable %s.\n",PROGRAM_ENVV);
  128.  
  129.    exit(1);
  130.    }
  131.  
  132. /*-/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\-*/
  133. /*-\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/-*/
  134.  
  135. /*------------------------------------------------------------------
  136.  * add default key words to reserved hash
  137.  *------------------------------------------------------------------*/
  138. void InitializeReservedHash(
  139.    Info *info,
  140.    char *keyList
  141.    )
  142.    {
  143.    int   i;
  144.    char *part;
  145.  
  146.    /*---------------------------------------------------------------
  147.     * create hash table
  148.     *---------------------------------------------------------------*/
  149.    info->reservedHash = HashCreate(sizeof(char *),
  150.                                   30,
  151.                                   (HashFunc *)IdentHash,
  152.                                   (ListCompareFunc *)IdentCompare,
  153.                                   cPostNoMem);
  154.  
  155.    if (!info->reservedHash)
  156.       cPostError(1,"error creating reserved word hash table");
  157.  
  158.    for (i=0; i<sizeof(ReservedTokens)/sizeof(char *); i++)
  159.       if (!HashAdd(info->reservedHash,&(ReservedTokens[i])))
  160.          cPostError(1,"error adding reserved word '%s' to hash table",
  161.                     ReservedTokens[i]);
  162.  
  163.    /*---------------------------------------------------------------
  164.     * loop through the comma separated keys ...
  165.     *---------------------------------------------------------------*/
  166.    part = strtok(keyList,",");
  167.    while (part)
  168.       {
  169.       /*------------------------------------------------------------
  170.        * special c++ token
  171.        *------------------------------------------------------------*/
  172.       if (!Stricmp("c++",part))
  173.          {
  174.          for (i=0; i<sizeof(CppReservedTokens)/sizeof(char *); i++)
  175.             if (!HashAdd(info->reservedHash,&(CppReservedTokens[i])))
  176.                cPostError(1,"error adding reserved word '%s' to hash table",
  177.                           CppReservedTokens[i]);
  178.  
  179.          }
  180.  
  181.       /*------------------------------------------------------------
  182.        * file name
  183.        *------------------------------------------------------------*/
  184.       else if (('@' == part[0]) && (1 != strlen(part)))
  185.          {
  186.          TokFileInfo  tfi;
  187.          char        *key;
  188.  
  189.          part++;
  190.  
  191.          tfi = TokFileOpen(part);
  192.  
  193.          if (!tfi)
  194.             cPostError(0,"error opening file '%s' for reading",part);
  195.  
  196.          else
  197.             {
  198.             while (NULL != (part = TokFileNext(tfi)))
  199.                {
  200.                key = malloc(1 + strlen(part));
  201.                if (!key)
  202.                   cPostError(1,"out of memory!!");
  203.  
  204.                strcpy(key,part);
  205.  
  206.                if (!HashAdd(info->reservedHash,&key))
  207.                   cPostError(0,"error adding reserved word '%s' to hash table; word ignored",
  208.                                 key);
  209.                }
  210.             }
  211.          }
  212.  
  213.       /*------------------------------------------------------------
  214.        * plain old